package com.example.Project.user;

import com.example.Project.Class.Apply;
import com.example.Project.user.userHttpMethods.userMethods;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.List;

public class LookApplyController {
    private static int id;
    void start() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("lookApply.fxml"));
        Scene scene = new Scene((Parent)fxmlLoader.load(), 761.0, 662.0);
        Stage stage=new Stage();
        stage.setTitle("社团信息管理");
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }
    void get(int theId){
        id=theId;
    }

    @FXML
    private Button checkpeople;

    @FXML
    private ContextMenu menu;

    @FXML
    private ContextMenu menu1;

    @FXML
    private TableView<Apply> table;

    @FXML
    private TableView<Apply> table1;
    @FXML
    private TableColumn<Apply, String> tableUserSex;

    @FXML
    private TableColumn<Apply, String> tableMassName;

    @FXML
    private TableColumn<Apply, String> tableMassName1;

    @FXML
    private TableColumn<Apply, String> tableUserName;

    @FXML
    private TableColumn<Apply, String> tableUserName1;

    @FXML
    private TableColumn<Apply, String > tableUserPhone;

    @FXML
    private TableColumn<Apply, String> tableUserPhone1;

    @FXML
    private TableColumn<Apply, String> tableUserSex1;
    @FXML
    private TableColumn<Apply, Integer> tableMassid;

    @FXML
    private TableColumn<Apply, Integer> tableMassid1;
    @FXML
    private TableColumn<Apply, Integer> tableUserid;

    @FXML
    private TableColumn<Apply, Integer> tableUserid1;
    com.example.Project.user.userHttpMethods.userMethods userMethods=new userMethods();
    @FXML
    public void initialize() throws ClassNotFoundException, IOException {
        table.getItems().clear();
        table1.getItems().clear();
        ObservableList<Apply> ApplyList= userMethods.getDelApply(id);
        ObservableList<Apply> ApplyList2=userMethods.getAddApply(id);
        if(ApplyList!=null||ApplyList2!=null) {
            System.out.println(ApplyList);
            Platform.runLater(() -> updateTableView2(ApplyList));
            Platform.runLater(() -> updateTableView(ApplyList2));
        }else{
            Alert al = new Alert(Alert.AlertType.CONFIRMATION);
            al.setHeaderText("你还不是社长，暂时无可管理的社团！！！");
            al.showAndWait();
        }
    }

    @FXML
    void lookPeople(ActionEvent event) throws IOException {
        ShowMassUserController enter=new ShowMassUserController();
        enter.get(id);
        enter.start();
    }
    private void updateTableView(List<Apply> list) {//添加
        this.table.getItems().addAll(list);
        this.tableMassName.setCellValueFactory(new PropertyValueFactory<>("massName"));
        this.tableUserName.setCellValueFactory(new PropertyValueFactory<>("name"));
        this.tableUserSex.setCellValueFactory(new PropertyValueFactory<>("sex"));
        this.tableUserPhone.setCellValueFactory(new PropertyValueFactory<>("phone"));
        this.tableMassid.setCellValueFactory(new PropertyValueFactory<>("massId"));
        this.tableUserid.setCellValueFactory(new PropertyValueFactory<>("userId"));
    }
    private void updateTableView2(List<Apply> list) {//删除
        this.table1.getItems().addAll(list);
        this.tableMassName1.setCellValueFactory(new PropertyValueFactory<>("massName"));
        this.tableUserName1.setCellValueFactory(new PropertyValueFactory<>("name"));
        this.tableUserSex1.setCellValueFactory(new PropertyValueFactory<>("sex"));
        this.tableUserPhone1.setCellValueFactory(new PropertyValueFactory<>("phone"));
        this.tableMassid1.setCellValueFactory(new PropertyValueFactory<>("massId"));
        this.tableUserid1.setCellValueFactory(new PropertyValueFactory<>("userId"));
    }

    public void btnYes(ActionEvent actionEvent) throws IOException {
        boolean re=userMethods.userSendDel(table.getSelectionModel().getSelectedItem().getUserId(),table.getSelectionModel().getSelectedItem().getMassId(),1);
        Alert al = new Alert(Alert.AlertType.CONFIRMATION);
        if(re){
            al.setHeaderText("已同意申请！！！");
        }else {
            al.setHeaderText("操作失败，请重新进行操作！！！");
        }
        al.showAndWait();
        table.getItems().clear();
        ObservableList<Apply> ApplyList2=userMethods.getAddApply(id);
        if(ApplyList2!=null) {
            Platform.runLater(() -> updateTableView(ApplyList2));
        }
    }

    public void btnNo(ActionEvent actionEvent) throws IOException {
        boolean re=userMethods.deleteMassUser(table.getSelectionModel().getSelectedItem().getUserId(),table.getSelectionModel().getSelectedItem().getMassId());
        Alert al = new Alert(Alert.AlertType.CONFIRMATION);
        if(re){
            al.setHeaderText("已拒绝申请！！！");
        }else {
            al.setHeaderText("操作失败，请重新进行操作！！！");
        }
        al.showAndWait();
        table.getItems().clear();
        ObservableList<Apply> ApplyList2=userMethods.getAddApply(id);
        if(ApplyList2!=null) {
            Platform.runLater(() -> updateTableView(ApplyList2));
        }
    }

    public void btnYes1(ActionEvent actionEvent) throws IOException {
        boolean re=userMethods.deleteMassUser(table1.getSelectionModel().getSelectedItem().getUserId(),table1.getSelectionModel().getSelectedItem().getMassId());
        Alert al = new Alert(Alert.AlertType.CONFIRMATION);
        if(re){
            al.setHeaderText("已同意申请！！！");
        }else {
            al.setHeaderText("操作失败，请重新进行操作！！！");
        }
        al.showAndWait();
        table1.getItems().clear();
        ObservableList<Apply> ApplyList2=userMethods.getDelApply(id);
        if(ApplyList2!=null) {
            Platform.runLater(() -> updateTableView2(ApplyList2));
        }
    }

    public void btnNo1(ActionEvent actionEvent) throws IOException {
        boolean re=userMethods.userSendDel(table1.getSelectionModel().getSelectedItem().getUserId(),table1.getSelectionModel().getSelectedItem().getMassId(),1);
        Alert al = new Alert(Alert.AlertType.CONFIRMATION);
        if(re){
            al.setHeaderText("已拒绝申请！！！");
        }else {
            al.setHeaderText("操作失败，请重新进行操作！！！");
        }
        al.showAndWait();
        al.showAndWait();
        table1.getItems().clear();
        ObservableList<Apply> ApplyList2=userMethods.getDelApply(id);
        if(ApplyList2!=null) {
            Platform.runLater(() -> updateTableView2(ApplyList2));
        }
    }
}
